package com.ph.irp.base.stockprice.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.stockprice.service.StockPriceService;
import com.ph.irp.dto.PageDTO;
import com.ph.irp.dto.StockPriceDTO;
import com.ph.irp.model.PBaseStockprice;
import com.ph.util.pagination.PageUtil;
import com.ph.util.pagination.ShowPageList;

public class StockPriceAction extends ActionSupport implements ShowPageList{
	// 采购价格service
	private StockPriceService stockPriceService = null;
	// 采购价格dto
	private StockPriceDTO stockPriceDTO = null;
	// 采购价格资料dto list
	private List<StockPriceDTO> stockPriceDtoList = null;
	
	private List<StockPriceDTO> tempStockPriceDtoList = null;
	// 采购价格资料model
	private PBaseStockprice pbaseStockPriceModel = null;
	// 接收jsp页面上传过来的id
	private Object[] rowlist = null;
	// 定位视图
	private String FORWORD = null;

	private PageDTO page;
	private String cpage;
	// 判断限价方式（针对某一供应商还是所有供应商）
	private int supplier;

	public PageDTO getPage() {
		return page;
	}

	public void setPage(PageDTO page) {
		this.page = page;
	}

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

	/**
	 * 动态查询采购价格
	 * 
	 * @return
	 */
	public String showStockPrices() throws Throwable {
		tempStockPriceDtoList = stockPriceService
				.getStockPrices(pbaseStockPriceModel);

		page = PageUtil.generatePageDTO(tempStockPriceDtoList, "showStockPricesPageList.action", cpage);
		stockPriceDtoList = page.getResList();
		pbaseStockPriceModel = null;

		FORWORD = "success";
		return FORWORD;
	}
	
	public String showPageList() throws Throwable {
		if (tempStockPriceDtoList != null) {
			page = PageUtil.generatePageDTO(tempStockPriceDtoList,
					"showCustomerPageList.action", cpage);
			stockPriceDtoList = page.getResList();
			FORWORD = "success";
		} else {
			FORWORD = "input";
		}
		return FORWORD;
	}

	/**
	 * action默认执行方法
	 */
	public String execute() throws Exception {

		try {
			return this.showStockPrices();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			throw new Exception(e.getMessage());
		}
	}

	/**
	 * 删除采购价格
	 * 
	 * @return
	 */
	public String deleteStockPrice() throws Throwable {
		// System.out.println(rowlist.length);

		stockPriceService.deleteStockPriceByIds(rowlist);
		pbaseStockPriceModel = null;
		// 定向视图
		FORWORD = "success";
		return FORWORD;
	}

	/**
	 * 新增、更新采购价格
	 * 
	 * @param stockPriceModel
	 * @return
	 */
	public String addStockPrice() throws Throwable {

		stockPriceService.addStockPrice(pbaseStockPriceModel);

		pbaseStockPriceModel = null;

		FORWORD = "success";
		return FORWORD;
	}

	public String editStockPrice() throws Throwable {

		stockPriceService.updateStockPrice(pbaseStockPriceModel);
		FORWORD = "success";
		return FORWORD;
	}

	/**
	 * 跳转到更新页面
	 * 
	 * @return
	 */
	public String updateStockPrice() throws Throwable {
		// 选中采购价格资料的id值
		Integer stockPriceId = Integer.valueOf((String) rowlist[0]);
		// 根据采购价格资料id查询采购价格资料dto
		stockPriceDTO = stockPriceService.getStockPriceDTOById(stockPriceId);
		pbaseStockPriceModel = stockPriceDTO.getStockpriceModel();
		FORWORD = "success";
		return FORWORD;
	}

	/**
	 * 跳转到新增页面
	 * 
	 * @return
	 */
	public String addInputStockPrice() throws Throwable {
		FORWORD = "success";
		return FORWORD;

	}

	/**
	 * 审核采购价格资料（只能审核一条）
	 * 
	 * @return
	 */
	public String checkStockPrice() throws Throwable {
		Integer stockPriceId = Integer.valueOf((String) rowlist[0]);

		stockPriceService.checkStockPrice(stockPriceId);

		pbaseStockPriceModel = null;
		FORWORD = "success";
		return FORWORD;
	}

	/**
	 * 反审核采购价格资料
	 * 
	 * @return
	 */
	public String unCheckStockPrice() throws Throwable {

		Integer stockPriceId = Integer.valueOf((String) rowlist[0]);
		stockPriceService.unCheckStockPrice(stockPriceId);
		pbaseStockPriceModel = null;
		FORWORD = "success";
		return FORWORD;
	}

	/**
	 * 跳转到采购价格限价页面
	 * 
	 * @return
	 */
	public String priceInputStockPrice() throws Throwable {

		Integer stockPriceId = Integer.valueOf((String) rowlist[0]);

		stockPriceDTO = stockPriceService.getStockPriceDTOById(stockPriceId);
		pbaseStockPriceModel = stockPriceDTO.getStockpriceModel();
		FORWORD = "success";
		return FORWORD;
	}

	public String controlPriceStockPrice() throws Throwable {

		this.stockPriceService.controlPrice(pbaseStockPriceModel, supplier);
		FORWORD = "success";
		return FORWORD;
	}

	/**
	 * getters and setters for instance variables
	 * 
	 * @return
	 */

	public StockPriceService getStockPriceService() {
		return stockPriceService;
	}

	public void setStockPriceService(StockPriceService stockPriceService) {
		this.stockPriceService = stockPriceService;
	}

	public StockPriceDTO getStockPriceDTO() {
		return stockPriceDTO;
	}

	public void setStockPriceDTO(StockPriceDTO stockPriceDTO) {
		this.stockPriceDTO = stockPriceDTO;
	}

	public List<StockPriceDTO> getStockPriceDtoList() {
		return stockPriceDtoList;
	}

	public void setStockPriceDtoList(List<StockPriceDTO> stockPriceDtoList) {
		this.stockPriceDtoList = stockPriceDtoList;
	}

	public PBaseStockprice getPbaseStockPriceModel() {
		return pbaseStockPriceModel;
	}

	public void setPbaseStockPriceModel(PBaseStockprice pbaseStockPriceModel) {
		this.pbaseStockPriceModel = pbaseStockPriceModel;
	}

	public Object[] getRowlist() {
		return rowlist;
	}

	public void setRowlist(Object[] rowlist) {
		this.rowlist = rowlist;
	}

	public int getSupplier() {
		return supplier;
	}

	public void setSupplier(int supplier) {
		this.supplier = supplier;
	}

}
